import {responseData} from "@example/views/chart/transform";

/**
 * 实现数据格式到echarts转换
 * @param records
 * @param columns
 * @returns {{xAxis: {data: *[]}, series: *[]}}
 */
export function genOption(records = [], columns = [],
                     {} = {}) {
  let xAxis = records.map(v => v[0])

  // console.log(xAxis)

  let series = []
  for (let i = 1; i < columns.length; i++) {
    let ret = {}
    ret.name = columns[i]
    ret.data = records.map(v => v[i])
    ret.smooth = true
    series.push(ret)
  }

  return {
    xAxis: {
      data: xAxis,
    },
    series
  }
}

export function genPieOptionSerie(records, {
  columnIndex = 0, dataIndex = 1
} = {}) {
  let record = records.map(v => v[dataIndex])
  let columns = records.map(v => v[columnIndex])
  let data = []
  for (let i = 0; i < columns.length; i++) {
    let ret = {}
    ret.name = columns[i]
    ret.value = record[i]
    data.push(ret)
  }

  return {
    data
  }
}

/**
 * 实现数据格式到表格转换
 * @param records
 * @param columns
 * @returns {{data: {}[], columns: {prop: *, label: *}[]}}
 */
export function genTableOption(records = [], columns = []) {
  return {
    columns: columns.map(v => {
      return {
        label: v,
        prop: v
      }
    }),
    data: records.map(v => {
      let ret = {}
      columns.forEach((column, index) => {
        ret[column] = v[index]
      })
      return ret;
    })
  }
}
